package com.wzd.mphelper.mphelpertest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wzd.mphelper.mphelpertest.consts.SuffixPreConsts;
import com.wzd.mphelper.mphelpertest.dto.request.SeedBeforePlanLandReqDTO;
import com.wzd.mphelper.mphelpertest.dto.response.SeedBeforePlanLandRespDTO;
import com.wzd.mphelper.mphelpertest.entity.SeedBeforePlanLand;
import com.wzd.mphelper.mphelpertest.entity.LandPlantingInfo;
import com.wzd.mphelper.mphelpertest.mapper.SeedBeforePlanLandMapper;
import com.wzd.mphelper.mphelpertest.service.ISeedBeforePlanLandService;
import com.wzd.mphelper.mphelpertest.subtable.service.CustomServiceImpl;
import com.wzd.mphelper.mphelpertest.subtable.shard.TableShardHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SeedBeforePlanLandServiceImpl extends CustomServiceImpl<SeedBeforePlanLandMapper, SeedBeforePlanLand> implements ISeedBeforePlanLandService {

    @Resource
    private SeedBeforePlanLandMapper seedBeforePlanLandMapper;

    @Override
    public void saveSeedBeforePlanLands(List<SeedBeforePlanLand> seedBeforePlanLands) {
        Map<String, List<SeedBeforePlanLand>> map = seedBeforePlanLands.stream().collect(Collectors.groupingBy(SeedBeforePlanLand::suffix));
        for (Map.Entry<String, List<SeedBeforePlanLand>> list : map.entrySet()) {
            saveBatch(list.getValue());
        }
    }

    /**
     * left join 包含有分表和未分表的数据，需要指定哪些表需要加分表后缀
     * mapper第一个参数为分表后缀
     * @param dto
     * @return
     */
    @Override
    public List<SeedBeforePlanLandRespDTO> queryAll(SeedBeforePlanLandReqDTO dto) {
        String suffix = SuffixPreConsts.SUFFIX_PRE+dto.getPlantCycleId();
        return (List<SeedBeforePlanLandRespDTO>) wrapSupplier(() -> seedBeforePlanLandMapper.testLeftJoin(suffix, dto), KVBuilder.init(SeedBeforePlanLand.class, suffix).put(LandPlantingInfo.class, suffix));
    }

    /**
     * TableShardHolder.putQueryTableShard()通过线程池指定分表后缀
     * @param dto
     * @return
     */
    @Override
    public List<SeedBeforePlanLand> list(SeedBeforePlanLandReqDTO dto) {
        TableShardHolder.putQueryTableShard(SuffixPreConsts.SUFFIX_PRE+dto.getPlantCycleId());
//        LambdaQueryWrapper<SeedBeforePlanLand> seedBeforePlanLandLambdaQw = new LambdaQueryWrapper<>();
//        seedBeforePlanLandLambdaQw.eq(StringUtils.isNotBlank(dto.getLandNo()), SeedBeforePlanLand::getLandNo, dto.getLandNo());
//        return list(seedBeforePlanLandLambdaQw);
        LambdaQueryWrapper<SeedBeforePlanLand> seedBeforePlanLandLambdaQw = new LambdaQueryWrapper<>();
        seedBeforePlanLandLambdaQw.eq(StringUtils.isNotBlank(dto.getLandNo()), SeedBeforePlanLand::getLandNo, dto.getLandNo());
        return seedBeforePlanLandMapper.selectList(seedBeforePlanLandLambdaQw);
    }


}
